package com.test.fundible.fundible_04_home;

import static com.test.fundible.fundible_00_utils.FundibleConfigs.PermissionCode_Camera;
import static com.test.fundible.fundible_00_utils.FundibleConfigs.PermissionCode_Home;
import static com.test.fundible.fundible_00_utils.FundibleConfigs.St_fundibleate_Wait;
import static com.test.fundible.fundible_00_utils.FundibleConfigs.State_Fa_fundibleil;
import static com.test.fundible.fundible_00_utils.FundibleConfigs.State_R_fundiblep;
import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.test.fundible.R;
import com.test.fundible.fundible_00_customer_service.Customer_fundibleServiceDialog;
import com.test.fundible.fundible_00_http.Http_fundibleLogger;
import com.test.fundible.fundible_00_utils.FundibleUtil;
import com.test.fundible.fundible_01_launcher.App_fundibleShow_fundibleCustomize;
import com.test.fundible.fundible_07_loan_status.Base_fundibleLnFragment;
import com.test.fundible.fundible_05_process.Pro_fundiblecessIn_fundiblefo;
import com.test.fundible.fundible_08_loan_repay.Ln_fundibleRpyFr_fundibleagment;
import com.test.fundible.fundible_07_loan_status.LoanStat_fundibleeFailFrag_fundiblement;
import com.test.fundible.fundible_07_loan_status.LoanSta_fundibleteWaitFra_fundiblegment;
import com.test.fundible.fundible_00_http.OkHttpUtil;
import com.test.fundible.fundible_10_vip.Pers_fundibleonalIn_fundiblefo;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainAct_fundibleivity extends AppCompatActivity implements EasyPermissions.RationaleCallbacks,EasyPermissions.PermissionCallbacks{

    private View applyloanView,dsView, meView;
    private ImageView applyloanIv,dsIv, meIv;
    private TextView applyloanTv,dsTv, meTv;

    private Base_fundibleLnFragment baseLnF_fundibleragment;
    private PersonalCenterF_fundibleragment pcFra_fundiblegment;
    private DataSaf_fundibleetyFragment dsFragm_fundibleent;

    private int page_fundibleFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fundible_main);
        //tag2024
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        applyloanView = findViewById(R.id.afm_view_applyloan);
        dsView = findViewById(R.id.afm_view_ds);
        meView = findViewById(R.id.afm_view_me);
        applyloanIv = findViewById(R.id.afm_iv_applyloan);
        dsIv = findViewById(R.id.afm_iv_ds);
        meIv = findViewById(R.id.afm_iv_me);
        applyloanTv = findViewById(R.id.afm_tv_applyloan);
        dsTv = findViewById(R.id.afm_tv_ds);
        meTv = findViewById(R.id.afm_tv_me);

        findViewById(R.id.afm_ib_customerservice).setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint("MAIN","CUSTOMER_SERVICE_CLICK","");
            new Customer_fundibleServiceDialog(this).show();
        });

        in_fundibleit();
    }

    private void in_fundibleit(){

        pcFra_fundiblegment = new PersonalCenterF_fundibleragment();
        dsFragm_fundibleent = new DataSaf_fundibleetyFragment();

        meView.setOnClickListener(v -> toM_fundiblee());
        dsView.setOnClickListener(v -> to_fundibleDs());
        applyloanView.setOnClickListener(v -> to_fundibleLn());

        //初始化用户状态
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction
                .add(R.id.afm_view_fragment, dsFragm_fundibleent)
                .add(R.id.afm_view_fragment, pcFra_fundiblegment)
                .commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("MAIN","START","");
        updateAkoma_fundiblepaLendUtil();
    }

    /**
     * 判断用户流程状态，如果状态有改变再更新页面
     */
    private void updateAkoma_fundiblepaLendUtil(){
        //判断用户流程状态
        String state = Pro_fundiblecessIn_fundiblefo.getInstance_fundible().getFundibleUtil();
        if (FundibleUtil.isStateChange_fundible(state)) updatePa_fundiblegeState(state);
    }

    private void updatePa_fundiblegeState(String userState) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (baseLnF_fundibleragment != null) fragmentTransaction.remove(baseLnF_fundibleragment);

        switch (userState){
            case State_R_fundiblep://还款状态
                baseLnF_fundibleragment = new Ln_fundibleRpyFr_fundibleagment();
                Pers_fundibleonalIn_fundiblefo.getInstance().setRepayFlag(true);
                Pers_fundibleonalIn_fundiblefo.getInstance().uploadUserInfo(null);

                //tag2024
                Ln_fundibleRpyFr_fundibleagment repayFregment = (Ln_fundibleRpyFr_fundibleagment) baseLnF_fundibleragment;
                repayFregment.setRefreshRunnable(() -> {
                    OkHttpUtil.getInstance_fundible().requestUsPrcs(() -> {
                        FundibleUtil.stopLoading();
                        updateAkoma_fundiblepaLendUtil();
                    });

                });

                break;
            case St_fundibleate_Wait://审核/放款中状态
                baseLnF_fundibleragment = new LoanSta_fundibleteWaitFra_fundiblegment();
                break;
            case State_Fa_fundibleil://被拒状态
                baseLnF_fundibleragment = new LoanStat_fundibleeFailFrag_fundiblement();
                break;
            default:
                baseLnF_fundibleragment = new HomeFr_fundibleagment();
                if (Pers_fundibleonalIn_fundiblefo.getInstance().getRepayFlag()){
                    Pers_fundibleonalIn_fundiblefo.getInstance().setRepayFlag(false);
                    //经验+12
                    Pers_fundibleonalIn_fundiblefo.getInstance().addExpValue(12);
                    Pers_fundibleonalIn_fundiblefo.getInstance().uploadUserInfo(null);
                }
        }

        fragmentTransaction.add(R.id.afm_view_fragment, baseLnF_fundibleragment);

        switch (page_fundibleFlag) {
            case 1:
                fragmentTransaction.show(dsFragm_fundibleent).hide(baseLnF_fundibleragment).hide(pcFra_fundiblegment);
                break;
            case 2:
                fragmentTransaction.show(pcFra_fundiblegment).hide(baseLnF_fundibleragment).hide(dsFragm_fundibleent);
                break;
            default:
                fragmentTransaction.show(baseLnF_fundibleragment).hide(dsFragm_fundibleent).hide(pcFra_fundiblegment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }

    private void to_fundibleDs(){

        OkHttpUtil.getInstance_fundible().uploadEventPoint("MAIN","DATA_SAFETY_CLICK","");

        page_fundibleFlag = 1;

        dsTv.setTextColor(getColor(R.color.green_light));
        applyloanTv.setTextColor(getColor(R.color.tab_gray));
        meTv.setTextColor(getColor(R.color.tab_gray));

        dsIv.setBackgroundResource(R.drawable.t_fundibleab_ds_li_fundibleght);
        applyloanIv.setBackgroundResource(R.drawable.t_fundibleab_ln_gr_fundibleay);
        meIv.setBackgroundResource(R.drawable.t_fundibleab_me_g_fundibleray);

        getSupportFragmentManager().beginTransaction()
                .show(dsFragm_fundibleent)
                .hide(baseLnF_fundibleragment)
                .hide(pcFra_fundiblegment)
                .commitAllowingStateLoss();

    }

    private void to_fundibleLn(){

        OkHttpUtil.getInstance_fundible().uploadEventPoint("MAIN","HOME_CLICK","");

        page_fundibleFlag = 0;

        dsTv.setTextColor(getColor(R.color.tab_gray));
        applyloanTv.setTextColor(getColor(R.color.green_light));
        meTv.setTextColor(getColor(R.color.tab_gray));

        dsIv.setBackgroundResource(R.drawable.ta_fundibleb_ds_gr_fundibleay);
        applyloanIv.setBackgroundResource(R.drawable.ta_fundibleb_ln_fundible_light);
        meIv.setBackgroundResource(R.drawable.t_fundibleab_me_g_fundibleray);

        getSupportFragmentManager().beginTransaction()
                .show(baseLnF_fundibleragment)
                .hide(dsFragm_fundibleent)
                .hide(pcFra_fundiblegment)
                .commitAllowingStateLoss();

    }

    private void toM_fundiblee(){

        OkHttpUtil.getInstance_fundible().uploadEventPoint("MAIN","ME_CLICK","");

        page_fundibleFlag = 2;

        dsTv.setTextColor(getColor(R.color.tab_gray));
        applyloanTv.setTextColor(getColor(R.color.tab_gray));
        meTv.setTextColor(getColor(R.color.green_light));

        dsIv.setBackgroundResource(R.drawable.ta_fundibleb_ds_gr_fundibleay);
        applyloanIv.setBackgroundResource(R.drawable.t_fundibleab_ln_gr_fundibleay);
        meIv.setBackgroundResource(R.drawable.t_fundibleab_me_li_fundibleght);

        getSupportFragmentManager().beginTransaction()
                .show(pcFra_fundiblegment)
                .hide(dsFragm_fundibleent)
                .hide(baseLnF_fundibleragment)
                .commitAllowingStateLoss();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FundibleUtil.clearState_fundible();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("MAIN","END","");
    }


    //---------------------权限处理--------------------

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode==PermissionCode_Camera && pcFra_fundiblegment!=null) pcFra_fundiblegment.onCameraPermissionGranted();
        else if (requestCode==PermissionCode_Home && baseLnF_fundibleragment!=null && baseLnF_fundibleragment instanceof HomeFr_fundibleagment)
            ((HomeFr_fundibleagment)baseLnF_fundibleragment).onPermissionsGranted();

    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PermissionCode_Camera || requestCode == PermissionCode_Home) {
            if (EasyPermissions.somePermissionPermanentlyDenied(this, perms))
                new AppSettingsDialog.Builder(this).build().show();
        }
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        if (requestCode == PermissionCode_Camera)
            EasyPermissions.requestPermissions(this,
                    getString(R.string.launch_permissions_camera_fundible),
                    PermissionCode_Camera,
                    Manifest.permission.CAMERA);
        else if (requestCode == PermissionCode_Home)
            EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions_all_fundible),
                    PermissionCode_Home, App_fundibleShow_fundibleCustomize.getInstance_fundible().getHomePermission_fundible());
    }

    @Override
    public void onRationaleDenied(int requestCode) {}


}